package cn.net.autocode.platform.menus;

import cn.net.autocode.core.bean.ResultT;
import cn.net.autocode.core.utils.UserThreadLocalUtils;
import cn.net.autocode.dbManager.Dao;
import cn.net.autocode.platform.menus.service.MenuServ;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/platform/userAuth")
public class PlatformUsesAuthCtrl {

    private final Dao dao;
    private final MenuServ menuServ;

    public PlatformUsesAuthCtrl(Dao dao, MenuServ menuServ) {
        this.dao = dao;
        this.menuServ = menuServ;
    }

    @RequestMapping("webAuth")
    public ResultT webAuth(@RequestBody JSONObject param){
        JSONArray organMenus = menuServ.organMenus(true);

        String orgCode = UserThreadLocalUtils.getOrgCode();
        String userName = param.getString("username");

        //获取授权菜单，defaultCheckedKeys

        //获取用户角色
        List<String> roleIds = dao.queryListValue("select role_id from sys_role_users where username=? and org_code=?",String.class,userName,orgCode);

        List<String> defaultCheckedKeys = new ArrayList<>();
        if (!roleIds.isEmpty()){
            defaultCheckedKeys = dao.queryListValue("select menu_id from sys_role_menus where role_id in (?)",String.class,roleIds);
        }

        //获取管理员菜单
        String orgManager = dao.queryValue("select org_manager from sys_organ where code=?",String.class,orgCode);
        if (userName.equalsIgnoreCase(orgManager)) {
            List<String> managerMenus = dao.queryListValue("select id from sys_menus t1 where t1.is_manager='Y' and (exists(select 1 from sys_organ_menu t2 where t1.id=t2.menu_id and t2.org_code=?) or t1.is_public='Y')",String.class,orgCode);
            defaultCheckedKeys.addAll(managerMenus);
        }

        JSONObject rs = new JSONObject();
        rs.put("data",organMenus);
        rs.put("defaultCheckedKeys",defaultCheckedKeys);
        return ResultT.success(rs);
    }

    @RequestMapping("appAuth")
    public ResultT appAuth(@RequestBody JSONObject param){
        JSONArray organMenus = menuServ.organAppMenus(true);

        String orgCode = UserThreadLocalUtils.getOrgCode();
        String userName = param.getString("username");


        //获取授权菜单，defaultCheckedKeys

        //获取用户角色
        List<String> roleIds = dao.queryListValue("select role_id from sys_role_users where username=? and org_code=?",String.class,userName,orgCode);

        List<String> defaultCheckedKeys = new ArrayList<>();
        if (!roleIds.isEmpty()){
            defaultCheckedKeys = dao.queryListValue("select menu_id from app_role_menus where role_id in (?)",String.class,roleIds);
        }

        JSONObject rs = new JSONObject();
        rs.put("data",organMenus);
        rs.put("defaultCheckedKeys",defaultCheckedKeys);
        return ResultT.success(rs);
    }

}
